/*
 * =====================================================================================
 *
 *       Filename:  timer.h
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2025/03/16 23时18分02秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  zhuzonghui(puchuchen@outlook.com) (), 
 *   Organization:  
 *
 * =====================================================================================
 */
#include<fuquOS/type.h>

#define US_TO_TICKS(us) ((us)*PIT_FREQUENCY/1000000)
#define MS_TO_TICKS(ms) ((ms)*PIT_FREQUENCY/1000)
#define SEC_TO_TICKS(sec) ((sec)*PIT_FREQUENCY)

void sleep_ticks(u32 ticks_to_wait);

#define sleep_us(ms) sleep_ticks(US_TO_TICKS(us))
#define sleep_ms(ms) sleep_ticks(MS_TO_TICKS(ms))
#define sleep_sec(sec) sleep_ticks(SEC_TO_TICKS(sec))
#define sleep(sec) sleep_sec(sec)

// CMOS寄存器端口
#define CMOS_ADDRESS 0x70
#define CMOS_DATA 0x71

// CMOS时间寄存器
#define CMOS_REG_SEC 0x00
#define CMOS_REG_MIN 0x02
#define CMOS_REG_HOUR 0x04
#define CMOS_REG_DAY 0x07
#define CMOS_REG_MON 0x08
#define CMOS_REG_YEAR 0x09
#define CMOS_REG_CENT 0x32

// 每个月的天数（普通年）
static const int days_in_month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };


typedef struct {
    int year;
    int month;
    int day;
    int hour;
    int min;
    int sec;
} Time;

u8 read_cmos(u8 reg);

int bcd_to_int(u8 bcd);

Time* get_utc_time();

Time* get_beijing_time();

int is_leap_year(int year);
